package com.glb.leetcode;

import java.util.Arrays;

public class C2_Remove {
  public static void main(String[] args) {
    int[] nums = { 3, 2, 2, 3, 1, 25 };
    System.out.println(Arrays.toString(nums));
    int result = removeElement(nums, 3);
    System.out.println(result);
    System.out.println(Arrays.toString(nums));
  }

  public static int removeElement(int[] nums, int val) {
    int left = 0;
    int right = nums.length;
    while (left < right) {
      if (nums[left] == val) {
        nums[left] = nums[right - 1];
        nums[right - 1] = val;
        right--;
      } else {
        left++;
      }
    }
    return left;
  }

  // public static int removeElement(int[] nums, int val) {
  // int length = nums.length;
  // int index = 0;
  // for(int i=0 ; i < length; i++) {
  // if (nums[i] != val) {
  // nums[index] = nums[i];
  // index++;
  // }
  // }
  // return index;
  // }

}
